package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.common.R;
import com.erp.entity.Goods;
import com.erp.entity.Order;
import com.erp.entity.Payable;
import com.erp.service.GoodsService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
@RequiresRoles(value = {"SupperAdministrator","LogisticsAdministrator"} ,logical = Logical.OR )
public class GoodsController {


    @Autowired
    private GoodsService goodsService;

    @PostMapping
    public R<String> save(@RequestBody Goods goods) {
        boolean save = goodsService.save(goods);
        if (!save) {
            return R.error("error");
        }
        return R.success("货物新增成功");
    }

    @DeleteMapping
    public R<String> delete(Long id) {
        boolean b = goodsService.removeById(id);
        if (!b) {
            return R.error("error");
        }
        return R.success("货物删除成功");
    }

    @GetMapping("/{id}")
    public R<Goods> find(@PathVariable Long id) {
        Goods byId = goodsService.getById(id);
        return R.success(byId);
    }

    @GetMapping("/page")
    public R<Page<Goods>> findpage(int page, int pageSize, String name) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Goods::getCreateDateTime);
        if (name!=null){
            lambdaQueryWrapper.like(Goods::getName,name);
        }
        Page<Goods> goodsPage = new Page<>(page,pageSize);
        goodsService.page(goodsPage,lambdaQueryWrapper);
        return R.success(goodsPage);
    }





    @PutMapping
    public R<String> update(@RequestBody Goods goods){

        boolean b = goodsService.updateById(goods);
        if (!b) {
            return R.error("error");
        }
        return R.success("货物更改成功");
    }


}
